/**
 * 用户相关 API
 */
import { get, put, del, post } from '../utils/request.js'
import { upload } from '../utils/request.js'
import { API_ENDPOINTS } from '../utils/config.js'
import { encryptPassword } from '../utils/auth.js'

/**
 * 获取当前用户信息
 * @returns {Promise} 用户信息
 */
export const getCurrentUser = () => {
  return get(API_ENDPOINTS.USER.ME)
}

/**
 * 更新当前用户信息
 * @param {Object} userInfo 用户信息
 * @param {string} userInfo.nickname 昵称
 * @param {string} userInfo.phone 手机号
 * @param {string} userInfo.email 邮箱
 * @returns {Promise} 更新结果
 */
export const updateCurrentUser = (userInfo) => {
  return put(API_ENDPOINTS.USER.UPDATE_ME, userInfo)
}

/**
 * 删除当前用户账户
 * @returns {Promise} 删除结果
 */
export const deleteCurrentUser = () => {
  return del(API_ENDPOINTS.USER.DELETE_ME)
}

/**
 * 修改密码
 * @param {string} currentPassword 当前密码
 * @param {string} newPassword 新密码
 * @returns {Promise} 修改结果
 */
export const changePassword = (currentPassword, newPassword) => {
  return post(API_ENDPOINTS.USER.CHANGE_PASSWORD, {
    currentPasswordMd5: encryptPassword(currentPassword),
    newPasswordMd5: encryptPassword(newPassword)
  })
}

/**
 * 上传用户头像
 * @param {string} filePath 文件路径
 * @returns {Promise} 上传结果
 */
export const uploadAvatar = (filePath) => {
  return upload(API_ENDPOINTS.USER.UPLOAD_AVATAR, filePath, {}, {
    name: 'avatarFile'
  })
}
